package com.jiaoyou.baojian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.entity.CardIndex;
import com.jiaoyou.baojian.entity.UserTrustInfo;
import com.jiaoyou.baojian.http.Api;
import com.jiaoyou.baojian.http.BaofangCallback;
import com.jiaoyou.baojian.http.response.BaseResponse;
import com.jiaoyou.baojian.otto.Event;
import com.squareup.otto.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

public class WalletZhuboActivity extends BaseActivity {

    @BindView(R.id.money)
    TextView moneyText;
    @BindView(R.id.candy)
    TextView candy;
    @BindView(R.id.tixian)
    Button tixian;
    @BindView(R.id.chongzhi)
    Button chongzhi;
    @BindView(R.id.linearLayout7)
    LinearLayout linearLayout7;
    @BindView(R.id.tixianshuoming)
    TextView tixianshuoming;

    private float money;

    private CardIndex cardIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        setTitle("钱包");

        refresh();

        needOtto();
    }

    @Subscribe
    public void onEvent(String event){
        if(Event.EVENT_ALIPAY.equals(event)){
            Log.i("EVENT_ALIPAY", "支付宝充值成功");
            refresh();
        } else if(Event.EVENT_WALLET_BIND_SUCCESS.equals(event)){
            Log.i("WALLET_BIND_SUCCESS", "提现账户绑定成功");
            refresh();
        }
    }

    private void refresh() {
        Api.getInstance().getCardIndex(new BaofangCallback<CardIndex>(){
            @Override
            public void onFailure(Call<BaseResponse<CardIndex>> call, Throwable t) {
                toast("获取钱包信息失败，请重试");
                onBackPressed();
            }

            @Override
            public void onSuccess(CardIndex response) {
                cardIndex = response;
                candy.setText(response.amount + "豆");
                money = response.withdrawAmountCash;
                String str = String.format("%.2f", money);
                moneyText.setText(str + "RMB");
                if(money < 100){
                    tixian.setEnabled(false);
                }
                tixianshuoming.setText(getResources().getString(R.string.tixian, money));
            }
        });
    }

    @Override
    public int getContentView() {
        return R.layout.activity_wallet_zhubo;
    }

    @OnClick({R.id.tixian, R.id.chongzhi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tixian:
                if(money < 100)
                {
                    toast("可提现额度不足100元");
                    return;
                }
                Intent intent = new Intent(this, TixianActivity.class);
                intent.putExtra("ketixian", cardIndex.withdrawAmountCash);
                if(cardIndex.userTrustInfoList != null && cardIndex.userTrustInfoList.size() > 0)
                    for(UserTrustInfo userTrustInfo : cardIndex.userTrustInfoList){
                        if(userTrustInfo.getType().equals("alipay")){
                            intent.putExtra("alipay", userTrustInfo);
                        }
                        if(userTrustInfo.getType().equals("weixin")){
                            intent.putExtra("wxpay", userTrustInfo);
                        }
                    }
                startActivity(intent);
                break;
            case R.id.chongzhi:
                RechargeActivity.show(this);
                break;
        }
    }
}
